<?php

class home extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('login_Model', '', TRUE);
	}

	function index() {
		if ($this->session->userdata('logged_in')) {
			$session_data = $this->session->userdata('logged_in');
			$data['login'] = $session_data['login'];
			$data['id'] = $session_data['id'];
			$data['first'] = $session_data['first'];
			$data['last'] = $session_data['last'];
			$data['email'] = $session_data['email'];
			$this->load->model('product_model');
			$products = $this->product_model->getAll();
			$data['products'] = $products;
			$this->load->view('home', $data);
		}
		else {
			redirect('login', 'refresh');
		}
	}

	function logout() {
		$this->session->unset_userdata('logged_in');
		$this->session->sess_destroy();
		redirect('login', 'refresh');
	}
	
	function delete() {
		$data = $this->session->userdata('logged_in');
		$id = $this->db->delete('customer', $data);
		$this->session->unset_userdata('logged_in');
		$this->session->sess_destroy();
		
		if ($id) {
			$this->session->set_flashdata('deletion_message', 'Your account has been deleted');
			redirect('login', 'refresh');
		}
		else {
			return false;
		}
	}
	
	function read($id) {
		$this->load->model('product_model');
		$product = $this->product_model->get($id);
		$data['product']=$product;
		$this->load->view('item.php',$data);
	}
}